package com.neuedu.outpatient01.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neuedu.outpatient01.entity.Disease;
import com.neuedu.outpatient01.entity.Mdiseaseandrecord;
import com.neuedu.outpatient01.entity.Medicaldisease;
import com.neuedu.outpatient01.entity.Medicaldiseaseandrecord;
import com.neuedu.outpatient01.entity.Medicalrecord;
import com.neuedu.outpatient01.entity.Register;
import com.neuedu.outpatient01.service.DiseaseService;
import com.neuedu.outpatient01.service.MedicaldiseaseService;
import com.neuedu.outpatient01.service.MedicalrecordService;
import com.neuedu.outpatient01.service.RegisterService;


@RestController
@RequestMapping("/outpatientHomepage")
public class PatientPersonalInformation {
	@Autowired
	private RegisterService service;
	@Autowired
	private DiseaseService serviced;
	@Autowired
	private MedicaldiseaseService servicedmd;
	@Autowired
	private MedicalrecordService servicedm;
	//用于该医生查询未填写患者
	@RequestMapping("/selectNoCheckedRegister")
	//http://localhost:8090/outpatientHomepage/selectNoCheckedRegister
	public List<Register> selectNoCheckedRegister(Register r){
			return service.selectNoCheckedRegister(r);
		
	}
	
	//用于查询表中是否有该患者的病历首页信息
	public Medicalrecord inquireHomeInfromation(Medicalrecord m) {
		return servicedm.inquireHomeInfromation(m);
	}
	//用于向表中添加患者病历首页信息
	public int insertHomeInfromation(Medicalrecord m) {
		return servicedm.insertHomeInfromation(m);
	}
	//用于向表中更新患者病历信息
	public int updateHomeInfromation(Medicalrecord m) {
		return servicedm.updateHomeInfromation(m);
	}
	//修改register表中的状态为已接诊
	public int updatestatus(int VisitState,String CaseNumber) {
		return service.updatestatus(VisitState,CaseNumber);
	}
	//向表中添加患者病历信息
	public int insertDiseaseInfor(Medicaldisease md) {
		return servicedmd.insertDiseaseInfor(md);
	}
	//向表中更新患者病历信息
	public int updateDiseaseInfor(Medicaldisease md) {
		return servicedmd.updateDiseaseInfor(md);
	}
	//根据疾病名称查询疾病的ID号
	public Disease inquirediseaseID(String d) {
		return serviced.inquirediseaseID(d);
	}
	//根据疾病的ID号查询疾病名称
	public Disease inquirediseaseName(int i) {
		return serviced.inquirediseaseName(i);
	}
	//根据病历号查询患者病历首页表信息
	public Medicalrecord inquireHistory(String CaseNumber) {
		return servicedm.inquireHistory(CaseNumber);
	}
	//根据病历号查询患者诊断表信息
	public Medicaldisease inquireHistoryMDisease(Integer MedicalID) {
		return servicedmd.inquireHistoryMDiseas(MedicalID);
	}
	//根据挂号查询患者诊断表信息
	public Medicaldisease inquireRegisterIDimage(Integer RegistID) {
		return servicedmd.inquireRegisterIDimage(RegistID);
	}
	//用于暂存门诊病历首页信息
	@RequestMapping("/temporaryHomeInfromation")
	//http://localhost:8090/outpatientHomepage/temporaryHomeInfromation?CaseNumber=600602&RegistID=5&Readme=肚子不舒服11111&DiseaseName=霍乱 NOS&DiagnoseType=1&CaseState=1
	public String temporaryHomeInfromation(Medicaldiseaseandrecord mad) {
		int diseaseID;
		if(inquirediseaseID(mad.getDiseaseName())==null) {
			return "没有这种疾病";
		}
		System.out.println("--mad="+mad);
		diseaseID = inquirediseaseID(mad.getDiseaseName()).getID();
		Medicalrecord m = new Medicalrecord();
		m.setCaseNumber(mad.getCaseNumber());
		m.setRegistID(mad.getRegistID());
		m.setReadme(mad.getReadme());
		m.setPresent(mad.getPresent());
		m.setPresentTreat(mad.getPresentTreat());
		m.setHistory(mad.getHistory());
		m.setAllergy(mad.getAllergy());
		m.setPhysique(mad.getPhysique());
		m.setProposal(mad.getProposal());
		m.setCareful(mad.getCareful());
		m.setCheckResult(mad.getCheckResult());
		m.setDiagnosis(mad.getDiagnosis());
		m.setHandling(mad.getHandling());
		m.setCaseState(mad.getCaseState());
		if(mad.getCaseState()==2) {
			updatestatus(2,m.getCaseNumber());	
	     }
		Medicaldisease md = new Medicaldisease();
		md.setRegistID(mad.getRegistID());
		md.setDiseaseID(diseaseID);
		md.setDiagnoseType(mad.getDiagnoseType());
		md.setGetSiskDate(mad.getGetSiskDate());
		md.setDiagnoseCate(mad.getDiagnoseCate());	
			if(inquireHomeInfromation(m) ==null) {
			insertHomeInfromation(m);
			Medicalrecord m1=inquireHomeInfromation(m);
		    md.setMedicalID(m1.getID());
			insertDiseaseInfor(md);
			return "成功";
		}else {
			if(inquireHomeInfromation(m).getCaseState()==2) {
				return "你已经提交了";
			}
			updateHomeInfromation(m);
			Medicalrecord m1=inquireHomeInfromation(m);
		    md.setMedicalID(m1.getID());
			updateDiseaseInfor(md);
			return "成功";
		}
	}
	//根据病历号查询历史看病日期
	public Register inquireRegisterDate(String CaseNumber) {
		return service.inquireRegisterDate(CaseNumber);
	}
	//根据病历号获得患者当前的信息
	@RequestMapping("/inquireHistoryMessage")
	//http://localhost:8090/outpatientHomepage/inquireHistoryMessage?CaseNumber=600602
	public Mdiseaseandrecord inquireHistoryMessage(Register re) {
		String CaseNumber = re.getCaseNumber();
		Mdiseaseandrecord mdis = new Mdiseaseandrecord();
		if(inquireHistory(CaseNumber)!=null) {
				Medicalrecord m=inquireHistory(CaseNumber);
				if(inquireHistoryMDisease(m.getID())!=null) {
			        Medicaldisease d=inquireHistoryMDisease(m.getID());
			        if(inquirediseaseName(d.getDiseaseID())!=null) {
			Disease d1 = inquirediseaseName(d.getDiseaseID());
			if(inquireRegisterDate(CaseNumber)!=null) {
	    	Register r = inquireRegisterDate(CaseNumber);
	    	mdis.setMedicalrecord(m);
		mdis.setMedicaldisease(d);
		mdis.setRegister(r);
		mdis.setDiseaseName(d1.getDiseaseName());
		 if(m.getCaseState()==3) {
			mdis.setState("诊毕");
	
		}else if(m.getCaseState()==1){
			mdis.setState("暂存");
	
		}else{
			mdis.setState("已提交");
		
		}
		
	    }
		}
		    }
		}
		return mdis;
	   
	}
}
